package com.qtcm.nas.spc.yejitongji.web;

import com.qtcm.nas.spc.pojo.*;
import com.qtcm.nas.spc.yejitongji.service.ImpOutStanDing_aService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description说明: TODO
 * @Date 2022/7/16 9:04
 * @PackageName:com.jzyh.spc.login.service
 * @ClassName: ImpUserService
 * @Author作者: Spc
 * @Version 1.0
 * @自己写的注释:
 */
@Controller("outStanDing_Controller")
@RequestMapping("/perIn")
public class OutStanDing_Controller {
    @Autowired
    @Qualifier("outStanDing_aService")
    private ImpOutStanDing_aService outStanDing_aService;

    //        查询所有信息并且传送到页面中   总的业绩统计
    @RequestMapping("/findAll_Out")
    public String findAll_OutStan(Model model, HttpSession session) {
        int huiyuano = 0;
        int feihuiyuan = 0;
        int dalibao = 0;
        int zonge = 0;
        int weixincis = 0;
        int weiZong = 0;
        System.out.println("----------------------------------------------------------");
        List<Outstanding_Aem> list = outStanDing_aService.findAll_OutStan();
        if (list.size() > 0) {
            for (Outstanding_Aem o : list
            ) {
                huiyuano += o.getI_Recharge_income();
                feihuiyuan += o.getI_Non_member_income();
                dalibao += o.getI_package_income();
                zonge += o.getI_Revenue_name();
            }
        }
        Outstanding_Aem outstanding_aem1 = new Outstanding_Aem();
        outstanding_aem1.setI_Recharge_income(huiyuano);
        outstanding_aem1.setI_Non_member_income(feihuiyuan);
        outstanding_aem1.setI_package_income(dalibao);
        outstanding_aem1.setI_Revenue_name(zonge);
//        System.out.println(outstanding_aem1.toString());
        model.addAttribute("outstanding_aem", outstanding_aem1);
//        ----------------------------------------------------------------------------
//        微信支付

        List<Outstanding_Aem> listWei = outStanDing_aService.findWeixin();
        int a = 0;
        for (Outstanding_Aem outstanding_Aem : listWei) {
            a++;
            weiZong += outstanding_Aem.getI_Non_member_income();
        }
        weixincis = a;
        System.out.println(a);
        model.addAttribute("weixincis", weixincis);
        model.addAttribute("weiZong", weiZong);
        //        现金支付
        int xianJincis = 0;
        int xianJinZong = 0;
        List<Outstanding_Aem> listXianjin = outStanDing_aService.findXianJin();
        int b = 0;
        for (Outstanding_Aem outstanding_Aem : listXianjin) {
            b++;
            xianJinZong += outstanding_Aem.getI_Non_member_income();
        }
        xianJincis = b;
        System.out.println(b);
        model.addAttribute("xianJincis", xianJincis);
        model.addAttribute("xianJinZong", xianJinZong);
        //        刷卡支付
        int shuaKacis = 0;
        int shuaKaZong = 0;
        List<Outstanding_Aem> listShuaka = outStanDing_aService.findShuaka();
        int c = 0;
        for (Outstanding_Aem outstanding_Aem : listShuaka) {
            c++;
            shuaKaZong += outstanding_Aem.getI_Non_member_income();
        }
        shuaKacis = c;
        System.out.println(c);
        model.addAttribute("shuaKacis", shuaKacis);
        model.addAttribute("shuaKaZong", shuaKaZong);
        //        体验卡支付
        int Tiyankacis = 0;
        int TiyankaZong = 0;
        List<Outstanding_Aem> listTiyanka = outStanDing_aService.findTiyanka();
        int d = 0;
        for (Outstanding_Aem outstanding_Aem : listTiyanka) {
            d++;
            TiyankaZong += outstanding_Aem.getI_Non_member_income();
        }
        Tiyankacis = d;
        System.out.println(d);
        model.addAttribute("Tiyankacis", Tiyankacis);
        model.addAttribute("TiyankaZong", TiyankaZong);
//        储值卡类型的个数
        Cards_within_cards_Out cards_within_cards_out = new Cards_within_cards_Out();
        cards_within_cards_out.setDushiGeshu(outStanDing_aService.findDushi());
        cards_within_cards_out.setBaijinGeshu(outStanDing_aService.findBaijin());
        cards_within_cards_out.setHongzuanGeshu(outStanDing_aService.findHongzuan());
        model.addAttribute("cards", cards_within_cards_out);
        return "business_Search";
    }

    //   添加数据
    @RequestMapping("/add_OutStan")
    public String add_OutStan(HttpSession session) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //        充值收入
        int chongZ = (Integer) session.getAttribute("chuzhika");
//        System.out.println("充值: " + chongZ);
//        非会员收入
        int feiHuiY = (Integer) session.getAttribute("noHuiy");
//        System.out.println("非会员收入: " + feiHuiY);
//        大礼包收入
        int daLiBao = (Integer) session.getAttribute("daoLb");
        System.out.println("大礼包收入: " + daLiBao);
//        总的收入
        int zongShouRu = (Integer) session.getAttribute("zongJinE");
//        System.out.println("总金额为: " + zongShouRu);
//        最后一位的支付方式
        String ZhiFuFangS = (String) session.getAttribute("ZhifuFS");
//        System.out.println("最后一位的支付方式: " + ZhiFuFangS);
        Outstanding_Aem outstanding_Aem = new Outstanding_Aem();
        outstanding_Aem.setI_Recharge_income(chongZ);
        outstanding_Aem.setI_Non_member_income(feiHuiY);
        outstanding_Aem.setI_package_income(daLiBao);
        outstanding_Aem.setI_Revenue_name(zongShouRu);
        outstanding_Aem.setI_Daily_time(sdf.format(new Date()));
        outstanding_Aem.setI_payment_method(ZhiFuFangS);
        if (outstanding_Aem.getI_package_income() == daLiBao) {
            return "forward:findAll_Out";
        } else {
            outStanDing_aService.addOutstan(outstanding_Aem);
            return "forward:findAll_Out";
        }


    }

    //    查询数据
    @RequestMapping("/findWaiBiao")
    public String findwaiBiao_OutStandard(HttpSession session) {
        //        储值卡
        int chuzhika = 0;
        List<Pre_sale_card_Out> listChuZ = outStanDing_aService.findChongZ();
//        System.out.println("储值卡为: " + listChuZ.size());
        for (Pre_sale_card_Out p : listChuZ) {
            chuzhika = (p.getZongJl() * p.getB_rechargeamount());
        }
        session.setAttribute("chuzhika", chuzhika);
//*******************************************************************************
//        年卡
        int nianka = 0;
//        消费总次数
        int XiaofeiCS = 0;
        List<Pre_sale_card_Out> list = outStanDing_aService.findNianK();
        for (Pre_sale_card_Out pre_sale_card : list) {
            nianka = (pre_sale_card.getZongJl() * pre_sale_card.getB_rechargeamount());
            XiaofeiCS += pre_sale_card.getZongJl();
        }
        session.setAttribute("XiaofeiCS", XiaofeiCS);
//        充值总的金额
        int chongZhi = (nianka + chuzhika);
        session.setAttribute("nianka", nianka);
        session.setAttribute("chongZhi", chongZhi);
//*******************************************************************************
//    非会员
        int noHuiy = 0;
//        支付方式
        String ZhifuFS = null;
        List<Orders_Out> listNoHY = outStanDing_aService.findNoHuiY();
        for (Orders_Out orders_Out : listNoHY) {
            noHuiy += orders_Out.getE_money();
        }
        List<Orders_Out> listZhifu = outStanDing_aService.findZhiFu();
        for (int i = 0; i < listZhifu.size(); i++) {
            if (i == list.size() - 1) {
                Orders_Out orders_out = listZhifu.get(i);
                ZhifuFS = orders_out.getE_Payment_method();
                session.setAttribute("ZhifuFS", ZhifuFS);
            }
        }
        session.setAttribute("noHuiy", noHuiy);
//*******************************************************************************
//        大礼包
        int daoLb = 0;
        List<Package_details_Out> listDaliBao = outStanDing_aService.findDaoliB();
        for (Package_details_Out package_details_Out : listDaliBao) {
            daoLb = (package_details_Out.getL_money() * package_details_Out.getL_frequency());
        }
        session.setAttribute("daoLb", daoLb);
//        总的金额
        int zongJinE = (daoLb + noHuiy + chongZhi);
        session.setAttribute("zongJinE", zongJinE);
//        ****************************************************************************
//       非会员的信息
        return "forward:add_OutStan";
    }




    //    根据日期进行查询所有业绩信息
    @RequestMapping("/dataFind")
    public String dateYejiMethod(String start,  String end, Model model) {
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        System.out.println(start+":"+end);
        int huiyuano = 0;
        int feihuiyuan = 0;
        int dalibao = 0;
        int zonge = 0;
        System.out.println("----------------------------------------------------------");
        List<Outstanding_Aem> listDate = outStanDing_aService.findDate_OutStan(start, end);
        System.out.println(listDate.size());
        if (listDate.size() > 0) {
            for (Outstanding_Aem o : listDate
            ) {
                huiyuano += o.getI_Recharge_income();
                feihuiyuan += o.getI_Non_member_income();
                dalibao += o.getI_package_income();
                zonge += o.getI_Revenue_name();
            }
        }
        Outstanding_Aem outstanding_aem1 = new Outstanding_Aem();
        outstanding_aem1.setI_Recharge_income(huiyuano);
        outstanding_aem1.setI_Non_member_income(feihuiyuan);
        outstanding_aem1.setI_package_income(dalibao);
        outstanding_aem1.setI_Revenue_name(zonge);
        System.out.println(outstanding_aem1.toString());
        model.addAttribute("outstanding_aem", outstanding_aem1);
//        ----------------------------------------------------------------------------
        //        微信支付
        int weixincis1 = 0;
        int weiZong1 = 0;
        List<Outstanding_Aem> listWei = outStanDing_aService.findWeixin();
        int a = 0;
        for (Outstanding_Aem outstanding_Aem : listWei) {
            a++;
            weiZong1 += outstanding_Aem.getI_Non_member_income();
        }
        weixincis1 = a;
        System.out.println(a);
        model.addAttribute("weixincis1", weixincis1);
        model.addAttribute("weiZong1", weiZong1);
        //        现金支付
        int xianJincis1 = 0;
        int xianJinZong1 = 0;
        List<Outstanding_Aem> listXianjin = outStanDing_aService.findXianJin();
        int b = 0;
        for (Outstanding_Aem outstanding_Aem : listXianjin) {
            b++;
            xianJinZong1 += outstanding_Aem.getI_Non_member_income();
        }
        xianJincis1 = b;
        System.out.println(b);
        model.addAttribute("xianJincis1", xianJincis1);
        model.addAttribute("xianJinZong1", xianJinZong1);
        //        刷卡支付
        int shuaKacis1 = 0;
        int shuaKaZong1 = 0;
        List<Outstanding_Aem> listShuaka = outStanDing_aService.findShuaka();
        int c = 0;
        for (Outstanding_Aem outstanding_Aem : listShuaka) {
            c++;
            shuaKaZong1 += outstanding_Aem.getI_Non_member_income();
        }
        shuaKacis1 = c;
        System.out.println(c);
        model.addAttribute("shuaKacis1", shuaKacis1);
        model.addAttribute("shuaKaZong1", shuaKaZong1);
        //        体验卡支付
        int Tiyankacis1 = 0;
        int TiyankaZong1 = 0;
        List<Outstanding_Aem> listTiyanka = outStanDing_aService.findTiyanka();
        int d = 0;
        for (Outstanding_Aem outstanding_Aem : listTiyanka) {
            d++;
            TiyankaZong1 += outstanding_Aem.getI_Non_member_income();
        }
        Tiyankacis1 = d;
        System.out.println(d);
        model.addAttribute("Tiyankacis1", Tiyankacis1);
        model.addAttribute("TiyankaZong1", TiyankaZong1);
        //        储值卡类型的个数
        Cards_within_cards_Out cards_within_cards_out = new Cards_within_cards_Out();
        cards_within_cards_out.setDushiGeshu(outStanDing_aService.findDushi());
        cards_within_cards_out.setBaijinGeshu(outStanDing_aService.findBaijin());
        cards_within_cards_out.setHongzuanGeshu(outStanDing_aService.findHongzuan());
        model.addAttribute("cards", cards_within_cards_out);
        return "business_Search1";
    }
}